﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HBAClient
{
    public partial class Banks : Form
    {
        private string strBankName;
        public Banks(string BankNameGivven)
        {
            strBankName = BankNameGivven;
            InitializeComponent();
        }

		private void Banks_Load(object sender, EventArgs e)
		{
            lblBankYear.Text = DateTime.Now.Year.ToString();
            lblBankName.Text = "Bank: " + strBankName;
            SetBankGrid();
		}
        private void SetBankGrid()
        {
            HBADBDataContext db = new HBADBDataContext();
            lblBankMsg.Text = Util.DEFAULT_BANK_MSG;

            var query = from bill in db.Bills
                        join bank in db.Banks on bill.BankID equals bank.BankID
                        where bill.UserID == Program.GetUserID() && bill.Date >= DateTime.Now.AddMonths(-1) && bank.BankName == strBankName
                        select new
                        {
                            BillID = bill.BillID,
                            BankName = bank.BankName,
                            Date = bill.Date,
                            Source = bill.Source,
                            InSum = bill.InSum,
                            OutSum = bill.OutSum,
                            Balance = bill.Balance
                        };


            dgvBanks.DataSource = query.OrderBy(x => x.Date);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelBill_Click(object sender, EventArgs e)
        {
            try
            {
                string strBillID_ToDelete = dgvBanks.SelectedCells[0].Value.ToString();
                DialogResult dl = MessageBox.Show("Are you sure you want to delete the record with BillID " +
                     strBillID_ToDelete  + "?", "Delete a record",
                    MessageBoxButtons.OKCancel);

                if (dl == DialogResult.OK)
                {
                    Util.DeleteBillRecord(Convert.ToInt64(strBillID_ToDelete));

                    lblBankMsg.Text = "Delete record is successfully";
                }
            }
            catch
            {
                lblBankMsg.Text = "Delete record is failed";
            }

            SetBankGrid();
        }
    }
}
